<?php 

namespace app\index\controller;
use \think\Db;
use \think\Controller;
use \think\Session;
use \think\Request;
use \think\View;
class voice{
	
	//青年新声显示页面
	public function youngAsk(){
		//获取提问数据
		$request = Request::instance();
		// $info = Db::field('u.username,u.userpic,y.image,y.asktime,y.praise')->name('user u,young_ask y')->where('u.uid','eq','y.aid')->select(); 
		$info = Db::name('user')->alias('u')->join('bbs_young_ask y','u.uid = y.uid')->field('u.username,u.userpic,y.image,y.content,y.asktime,y.praise')->select();
		
		
		return success_re('成功',$info);

		
	}

	//发起青年新声提问页面
	public function postAsk(){
		
		$request = Request::instance();

		$content = $request->param('content');
		$time = time();
		$is_open = $request->param('is_open');
		$uid = $request->param('uid');

		$data['content'] = $content;
		$data['asktime'] = $time;
		$data['is_open'] = $is_open;
		$data['uid'] = $uid;

		$res = Db::name('young_ask')->insert($data);
		if(!$res){

			return error('失败');
		}else{
			return success_re('成功',$res);
		}
			}
		

	//提问详情页面
	public function askDetail(){

		$request = Request::instance();
		$ask_id  = $request->param('ask_id');
		$young_ask_info = Db::name('young_ask')->where('ask_id','eq',$ask_id)->find();
		//var_dump($young_ask_info);
		$user_id  = $young_ask_info['uid'];
		$user_info = Db::name('user')->where('uid','eq',$user_id)->field('username,userpic')->find();
		//var_dump($user_info);
		$ask_content_info = Db::name('ask_content')->where('ask_id','eq',$ask_id)->field('cid,content,time')->find();

		$company_info = Db::name('company')->where('cid','eq',$ask_content_info['cid'])->field('cname,image')->find();
		$data2['user_info'] = $user_info;
		$data2['ask_content_info'] = $ask_content_info;
		$data2['company_info'] = $company_info;

		return success_re('成功',$data2);
	}
	//合理化建议页面
	public function advice(){

		$request = Request::instance();
		$res = Db::name('advice')->alias('a')->join('user u','a.uid = u.uid')->field('u.username,u.userpic,a.time,a.image,a.content')->select();

		return success_re('成功',$res);

		
	}
	//发布合理化建议页面
	public function postAdvice(){
		$request = Request::instance();
		$uid = $request->param('uid');
		$title = $request->param('title');
		$content = $request->param('content');
		$time = time();

		//重组数据
		$data['uid'] = $uid;
		$data['title'] = $title;
		$data['content'] = $content;
		$data['time'] = $time;

		$res = Db::name('advice')->insert($data);
		if(!$res){
			$this->error('发送失败');
		}else{
			$this->success('发送成功');
		}

		
	}


	//合理化建议详情页面
	public function adviceDetail(){
		//echo 1234;
		$request = Request::instance();

		$uid  = $request->param('uid');
		$advice_id = $request->param('advice_id');
		// $data = Db::name('advice')->alias('a')->join('user u',"a.id = $advice_id","u.uid = $uid")->select();
		 $advice_info = Db::name('advice')->where('id','eq',$advice_id)->select();
		 $user_info = Db::name('user')->where('uid','eq',$uid)->select();
		 $data['advice_info'] = $advice_info;
		 $data['user_info'] = $user_info;

		return $data;

	}


	//投票调查
	public function vote(){
		$request = Request::instance();
		$res = Db::name('vote')->field('title')->select();
		return $res;
	}

	//投票详情页面
	public function voteDetail(){

		$request = Request::instance();
		$vote_id = $request->param('vote_id');
		$res = Db::name('vote')->alias('v')->join('vote_question q','v.vote_id = q.question_id')->join('vote_select s','s.question_id = q.question_id')->select();
		//var_dump($res);
		return $res;
	} 

	//发布投票调查
	public function postVote(){
		$request = Request::instance();
		$admin_id = $request->param('admin_id');
		$title = $request->param('title');
		$time = time();
		$description = $request->param('discription');
		
		$data['admin_id'] = $admin_id;
		$data['title'] = $title;
		$data['content'] = $content;
		$data['time'] = $time;
		$res = Db::name('vote')->insert($data);

		if(!$res){

			$this->error('发送失败');
		}else{
			$this->success('发送成功');
		}


	}
}